package GUI;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.Font;
import javax.swing.JScrollPane;
import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.getReqestFile;

import java.util.Iterator;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;		
import javax.swing.JComboBox;

import common.MyFile;

import java.awt.event.ActionEvent;
import java.io.FileOutputStream;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ShowAllRequests extends IS_dep_manager{

	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private JComboBox<String> processBox;
	private GetAllPack prcPck,req;
	private JTextArea ChanReq,info,situDesc,Reason,Comment;
	private int selectedReq;
	private getReqestFile gerReqFilePck;
	private String fileNameCheck;

	
	public ShowAllRequests(MainGUI mainGui) {
		super(mainGui);
		this.mainGui= mainGui;
		selectedReq=0;
		getAllProccessSelect();
		fileNameCheck=null;
		JLabel lblInformationSystem = new JLabel("Information System");
		lblInformationSystem.setBounds(272, 148, 152, 20);
		getContentPane().add(lblInformationSystem);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(434, 186, 342, 83);
		getContentPane().add(scrollPane);
		
		situDesc = new JTextArea();
		situDesc.setEditable(false);
		scrollPane.setViewportView(situDesc);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(434, 279, 342, 83);
		getContentPane().add(scrollPane_1);
		
		 ChanReq = new JTextArea();
		 ChanReq.setEditable(false);
		scrollPane_1.setViewportView(ChanReq);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(434, 373, 342, 83);
		getContentPane().add(scrollPane_2);
		
		Reason = new JTextArea();
		Reason.setEditable(false);
		scrollPane_2.setViewportView(Reason);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(434, 467, 342, 83);
		getContentPane().add(scrollPane_3);
		
		Comment = new JTextArea();
		Comment.setEditable(false);
		scrollPane_3.setViewportView(Comment);
		
		JLabel lblNewLabel = new JLabel("Situation Description");
		lblNewLabel.setBounds(272, 185, 112, 14);
		getContentPane().add(lblNewLabel);
		
		JLabel lblChangeRequestedDescription = new JLabel("Change Requested Description");
		lblChangeRequestedDescription.setBounds(272, 278, 152, 14);
		getContentPane().add(lblChangeRequestedDescription);
		
		JLabel lblReasonForRequest = new JLabel("Reason for Request");
		lblReasonForRequest.setBounds(272, 372, 141, 14);
		getContentPane().add(lblReasonForRequest);
		
		JLabel lblComments = new JLabel("Comments");
		lblComments.setBounds(272, 466, 92, 14);
		getContentPane().add(lblComments);
		
		JButton btnFile = new JButton("Open File");
		btnFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(selectedReq==0)
				{
				JOptionPane.showMessageDialog(serverGUI.mainframe,"You Havn't Selected A Process Yet");	
				}
				else if(fileNameCheck==null)
				{
				JOptionPane.showMessageDialog(serverGUI.mainframe,"This Request Don't Have An Attached File");		
				}
				else
				{	
				gerReqFilePck = new getReqestFile(selectedReq);
				gerReqFilePck = treatGetReqestFilePack(gerReqFilePck);
				if(gerReqFilePck.getFileFlag()==1)
					{
					FileInput(gerReqFilePck.getFile(),gerReqFilePck.getFileName()+"Attachment."+gerReqFilePck.getFileEnd());
					}
				}
			}

			private getReqestFile treatGetReqestFilePack(getReqestFile gerReqFilePck) {
				ShowAllRequests.this.mainGui.client.handleMessageFromClientUI(gerReqFilePck);
				return((getReqestFile)(ShowAllRequests.this.mainGui.client.getMessage()));
				}
		});
		btnFile.setBounds(434, 561, 89, 23);
		getContentPane().add(btnFile);
		
		JLabel lblOpenAttachedFile = new JLabel("Open Attached File");
		lblOpenAttachedFile.setBounds(272, 565, 112, 14);
		getContentPane().add(lblOpenAttachedFile);
		
		
		processBox = new JComboBox<String>();
		processBox.addItem("");
		
		Iterator<String> iter = prcPck.nArray.iterator();
		
		while (iter.hasNext()) {
			String procId = iter.next();
				processBox.addItem(procId);
		}
		processBox.setBounds(490, 79, 141, 23);
		getContentPane().add(processBox);
		
		processBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				getDetails();
				Iterator<String> iter1 =  req.nArray.iterator();
					String data = iter1.next();
					info.setText(data); 
					data= iter1.next();
					situDesc.setText(data);
					data= iter1.next();
					ChanReq.setText(data);
					data= iter1.next();
					Reason.setText(data);
					data= iter1.next();
					Comment.setText(data);
					data=iter1.next();
					fileNameCheck=data;
			}	 
		}); 
	
		
		JLabel lblChooseProcess = new JLabel("Choose Process");
		lblChooseProcess.setBounds(356, 83, 112, 14);
		getContentPane().add(lblChooseProcess);
		
		JLabel lblDetails = new JLabel("Details:");
		lblDetails.setFont(new Font("Tahoma", Font.BOLD, 19));
		lblDetails.setBounds(272, 102, 152, 37);
		getContentPane().add(lblDetails);
		
		info = new JTextArea();
		info.setEditable(false);
		info.setBounds(434, 152, 342, 23);
		getContentPane().add(info);
	
	}
	public void getAllProccessSelect(){
		prcPck = new GetAllPack(MessagePack.getAllprocess);
		mainGui.client.handleMessageFromClientUI(prcPck);
		prcPck = (GetAllPack) mainGui.client.getMessage();	
	}
	
	
	public void getDetails(){
		req = new GetAllPack(MessagePack.getAllRequests);
		int p= Integer.parseInt((String) processBox.getSelectedItem());
		req.InitID = p;
		mainGui.client.handleMessageFromClientUI(req);
		req = (GetAllPack) mainGui.client.getMessage();
		selectedReq=p;
	}
	
	public void FileInput(Object msg,String fileName)
  {
	  int fileSize =((MyFile)msg).getSize();	  
	  byte [] picbytearray  =((MyFile)msg).getMybytearray();
	  FileOutputStream out;
	  try
	  	{
		out=new FileOutputStream(fileName);
		out.write(((MyFile)msg).getMybytearray());
	    out.close();
	  	}
	  catch(Exception e)
	  	{
	      System.out.println("ERROR - FileNotFound!");   
	  	}
	  System.out.println("length "+ fileSize);
	  }
	
	
}